package Blog::Controller::Posts;

use strict;
use warnings;
use parent 'Catalyst::Controller::HTML::FormFu';
use Data::Dumper;

=head1 NAME

Blog::Controller::Posts - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller for posts on the blog

=head1 METHODS

=cut


=head2 list

Fetch all public post objects and pass to posts/list.tt2 in stash to be displayed

=cut

sub list : Local {
  my ($self, $c) = @_;

  # Retrieve all of the book records as book model objects and store in the
  # stash where they can be accessed by the TT template
  $c->stash->{posts} = [$c->model('DB::Post')->public->newest_first];

  # Set the TT template to use.  You will almost always want to do this
  # in your action methods (action methods respond to user input in
  # your controllers).
  $c->stash->{template} = 'posts/list.tt2';
}

=head2 display_post

Display a single specific post

=cut

sub display_post :Local :Args(1){
  my ($self, $c, $post_id) = @_;

  $c->stash->{resultset} = $c->model('DB::Post');
  my $post = $c->model('DB::Post')->find($post_id);

  $c->stash->{post}      = $post;
  $c->stash->{template}  = 'posts/display_post.tt2';
}

=head2 admin_list

Fetch all post objects and pass to posts/admin_list.tt2 in stash to be displayed

=cut

sub admin_list : Local {
  my ($self, $c) = @_;

  # Retrieve all of the book records as book model objects and store in the
  # stash where they can be accessed by the TT template
  $c->stash->{posts} = [$c->model('DB::Post')->newest_first];

  # Set the TT template to use.  You will almost always want to do this
  # in your action methods (action methods respond to user input in
  # your controllers).
  $c->stash->{template} = 'posts/admin_list.tt2';
}

=head2 add_post

Fetch all post objects and pass to posts/list.tt2 in stash to be displayed

=cut
sub add_post : Local :Args(0) :FormConfig {
  my ($self, $c) = @_;

  my $form = $c->stash->{form};

  if ($form->submitted_and_valid) {
    my $post = $c->model('DB::Post')->new_result({});

    $form->model->update($post);
    my $msg = "Added post [" . $post->title . "] with id [" . $post->id . "]";
    $c->flash->{status_msg} = $msg;
    $c->log->info($msg);

    $c->response->redirect($c->uri_for($self->action_for('admin_list')));
    $c->detach;
  }
  else {
    # In the future should generate a list of registered authors
    # or other options that should be picked from a dropdown
  }

  $c->stash->{template} = 'posts/add_post.tt2';
}

sub edit_post :Local :Args(1) :FormConfig('posts/add_post.yml') {
  my ($self, $c, $post_id) = @_;

  # Store the ResultSet in stash so it's available for other methods
  $c->stash->{resultset} = $c->model('DB::Post');

  my $post = $c->stash->{resultset}->find($post_id);
  $c->log->debug("post.title [" . $post->{title} . "]");
  $c->log->debug("post.author [" . $post->{author} . "]");
  $c->log->debug("post.body [" . $post->{body} . "]");

  my $form = $c->stash->{form};
  #$c->log->debug("Form object dump [" . Dumper($form) . "]");

  if ($form->submitted_and_valid) {
    $form->model->update($post);
    $c->response->redirect($c->uri_for($self->action_for('admin_list')));
    $c->detach;
  }
  else {
    $c->log->debug("In the else block");
    $form->model->default_values($post);
  }

  $c->stash->{template} = 'posts/add_post.tt2';
}

=head2 delete

Delete the specified post from the database

=cut

sub delete :Local :Args(1) {
  my ($self, $c, $post_id) = @_;
  
  my $post = $c->model('DB::Post')->find($post_id);
  my $title = $post->title;
  $post->delete;
  my $msg = "Deleted post [$title] with id [$post_id]";
  $c->log->info($msg);

  $c->flash->{status_msg} = $msg;

  # Go back to the admin post list
  $c->response->redirect($c->uri_for($self->action_for('admin_list')));
}

=head1 AUTHOR

P. Michael Holland C<maikeru.horando at gmail.com>

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
